﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
using System.Collections.Generic;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据访问类:ch_zhoubian
    /// </summary>
    public partial class zhoubian
    {
        public zhoubian()
        { }
        #region 基本方法============================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_zhoubian");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.zhoubian model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into ch_zhoubian(");
                        strSql.Append("category_id,title,remark,img_url,user_id,quanzi_id,lianxiren,mobile,add_time,hits,status)");
                        strSql.Append(" values (");
                        strSql.Append("@category_id,@title,@remark,@img_url,@user_id,@quanzi_id,@lianxiren,@mobile,@add_time,@hits,@status)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					        new SqlParameter("@category_id", SqlDbType.Int,4),
					        new SqlParameter("@title", SqlDbType.NVarChar,100),
					        new SqlParameter("@remark", SqlDbType.NVarChar,255),
                            new SqlParameter("@img_url", SqlDbType.NVarChar,100),
					        new SqlParameter("@user_id", SqlDbType.Int,4),
					        new SqlParameter("@quanzi_id", SqlDbType.Int,4),
					        new SqlParameter("@lianxiren", SqlDbType.NVarChar,50),
					        new SqlParameter("@mobile", SqlDbType.NVarChar,20),
					        new SqlParameter("@add_time", SqlDbType.DateTime),
					        new SqlParameter("@hits", SqlDbType.Int,4),
					        new SqlParameter("@status", SqlDbType.TinyInt,1)};

                        parameters[0].Value = model.category_id;
                        parameters[1].Value = model.title;
                        parameters[2].Value = model.remark;
                        parameters[3].Value = model.img_url;
                        parameters[4].Value = model.user_id;
                        parameters[5].Value = model.quanzi_id;
                        parameters[6].Value = model.lianxiren;
                        parameters[7].Value = model.mobile;
                        parameters[8].Value = model.add_time;
                        parameters[9].Value = model.hits;
                        parameters[10].Value = model.status;
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);//插入后赋值
                        #endregion
                        #region 添加图片相册====================
                        if (model.albums != null)
                        {
                            new DAL.common_albums().Add(conn, trans, model.albums, 4, model.id);
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.zhoubian model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update ch_zhoubian set ");

                        strSql.Append("category_id = @category_id,");
                        strSql.Append("title = @title,");
                        strSql.Append("remark = @remark,");
                        strSql.Append("img_url = @img_url,");
                        strSql.Append("user_id = @user_id,");
                        strSql.Append("quanzi_id = @quanzi_id,");
                        strSql.Append("lianxiren = @lianxiren,");
                        strSql.Append("mobile = @mobile,");
                        strSql.Append("add_time = @add_time,");
                        strSql.Append("hits = @hits,");
                        strSql.Append("status = @status");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
			                new SqlParameter("@id", SqlDbType.Int,4),
                            new SqlParameter("@category_id", SqlDbType.Int,4),
                            new SqlParameter("@title", SqlDbType.NVarChar,100),
                            new SqlParameter("@remark", SqlDbType.NVarChar,255),
                            new SqlParameter("@img_url", SqlDbType.NVarChar,100),
                            new SqlParameter("@user_id", SqlDbType.Int,4),
                            new SqlParameter("@quanzi_id", SqlDbType.Int,4),
                            new SqlParameter("@lianxiren", SqlDbType.NVarChar,50),
                            new SqlParameter("@mobile", SqlDbType.NVarChar,20),
                            new SqlParameter("@add_time", SqlDbType.DateTime),
                            new SqlParameter("@hits", SqlDbType.Int,4),
                            new SqlParameter("@status", SqlDbType.TinyInt,1)};

                        parameters[0].Value = model.id;
                        parameters[1].Value = model.category_id;
                        parameters[2].Value = model.title;
                        parameters[3].Value = model.remark;
                        parameters[4].Value = model.img_url;
                        parameters[5].Value = model.user_id;
                        parameters[6].Value = model.quanzi_id;
                        parameters[7].Value = model.lianxiren;
                        parameters[8].Value = model.mobile;
                        parameters[9].Value = model.add_time;
                        parameters[10].Value = model.hits;
                        parameters[11].Value = model.status;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改图片相册==========================
                        //删除/添加/修改相册图片
                        new DAL.common_albums().Update(conn, trans, model.albums, 4, model.id);
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int channel_id, int article_id)
        {
            //取得相册MODEL
            List<Model.common_albums> albumsList = new DAL.common_albums().GetList(channel_id, article_id);

            //删除图片相册
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from ch_common_albums");
            strSql2.Append(" where channel_id=@channel_id and article_id=@article_id");
            SqlParameter[] parameters2 = {
                    new SqlParameter("@channel_id", SqlDbType.Int,4),
					new SqlParameter("@article_id", SqlDbType.Int,4)};
            parameters2[0].Value = channel_id;
            parameters2[1].Value = article_id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            //删除主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_zhoubian");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = article_id;
            cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                new DAL.common_albums().DeleteFile(albumsList); //删除图片
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.zhoubian GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,category_id,title,remark,img_url,user_id,quanzi_id,lianxiren,mobile,add_time,hits,status");
            strSql.Append(" from ch_zhoubian");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.zhoubian model = new Model.zhoubian();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,category_id,title,remark,img_url,user_id,quanzi_id,lianxiren,mobile,add_time,hits,status");
            strSql.Append(" FROM ch_zhoubian ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 根据分类ID获得前几行数据
        /// </summary>
        public DataSet GetList(int category_id, int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,category_id,title,remark,img_url,user_id,quanzi_id,lianxiren,mobile,add_time,hits,status");
            strSql.Append(" FROM ch_zhoubian ");
            if (category_id > 0)
            {
                strSql.Append(" where category_id =" + category_id);
            }
            if (strWhere.Trim() != "")
            {
                if (category_id > 0)
                {
                    strSql.Append(" and " + strWhere);
                }
                else
                {
                    strSql.Append(" where " + strWhere);
                }
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int category_id, int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ch_zhoubian");
            if (category_id > 0)
            {
                strSql.Append(" where category_id =" + category_id);
            }
            if (strWhere.Trim() != "")
            {
                if (category_id > 0)
                {
                    strSql.Append(" and " + strWhere);
                }
                else
                {
                    strSql.Append(" where " + strWhere);
                }
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion

        #region 扩展方法============================
        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetTitle(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 title from ch_zhoubian");
            strSql.Append(" where id=" + id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "-";
            }
            return title;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_zhoubian set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.zhoubian DataRowToModel(DataRow row)
        {
            Model.zhoubian model = new Model.zhoubian();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["category_id"] != null && row["category_id"].ToString() != "")
                {
                    model.category_id = int.Parse(row["category_id"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
                if (row["img_url"] != null)
                {
                    model.img_url = row["img_url"].ToString();
                }
                if (row["user_id"] != null && row["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(row["user_id"].ToString());
                }
                if (row["quanzi_id"] != null && row["quanzi_id"].ToString() != "")
                {
                    model.quanzi_id = int.Parse(row["quanzi_id"].ToString());
                }
                if (row["lianxiren"] != null)
                {
                    model.lianxiren = row["lianxiren"].ToString();
                }
                if (row["mobile"] != null)
                {
                    model.mobile = row["mobile"].ToString();
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["hits"] != null && row["hits"].ToString() != "")
                {
                    model.hits = int.Parse(row["hits"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                //相册信息
                model.albums = new DAL.common_albums().GetList(4, model.id);
            }
            return model;
        }
        #endregion
    }
}
